#include "T9manager.h"


time_t BeginTimer()
{
    //timer declaration
    time_t Begin; //initialize Begin

    time(&Begin); //start the timer

    return Begin;
}
time_t ElapsedTimer(time_t Begin)
{
    time_t End;
    time(&End);   //stop the timer
    return ((End-Begin));
}


//This routine initializes all variables you need to control
void initData(Data *d){
	d->listat9=NULL;
	d->currentnode=NULL;
	d->currentword=NULL;
	d->tempstring=NULL;
	d->tempnum=NULL;
	d->tempchar=NULL;
	d->punctuation=NULL;
	d->symbols=NULL;
	d->charnumber=0;
	d->numflag=false;	
	d->tempstring=(char*)malloc(wordlen*sizeof(char));
	d->tempstring[0]='\0';
	d->tempnum=(char*)malloc(wordlen*sizeof(char));
	d->tempnum[0]='\0';
	d->tempchar=(char*)malloc(wordlen*sizeof(char));
	d->tempchar[0]='\0';
	d->punctuation=(char*)malloc(wordlen*sizeof(char));
	d->symbols=(char*)malloc(wordlen*sizeof(char));
	d->begin=BeginTimer();
	d->lastClickedButton=-1;
	d->state=0;   	
	
}


struct elementonumerico* inserisciNuovoNodo(struct elementonumerico **nuovoNodo,struct elementonumerico* elementoprev){
	struct elementonumerico *newnode;
	newnode=(struct elementonumerico*)malloc(sizeof(struct elementonumerico));
	newnode->element2=NULL;
	newnode->element3=NULL;
	newnode->element4=NULL;
	newnode->element5=NULL;
	newnode->element6=NULL;
	newnode->element7=NULL;
	newnode->element8=NULL;
	newnode->element9=NULL;
	newnode->elementoprec=elementoprev;
	newnode->paroleHead=NULL;
	newnode->paroleTail=NULL;	
	nuovoNodo=&newnode; 
	return newnode;
}

void inserisciParola(struct parola **head,struct parola **tail,char *str,int count){


	struct parola *newNode;
	struct parola *temp=*head;
	newNode=(struct parola*)malloc(sizeof(struct parola));
	char *newWord=(char *)malloc(wordlen*sizeof(char));
	strcpy(newWord,str);
	newNode->str=newWord;
	newNode->count=count;
	if (*head==NULL){
		//printf("\ncreo nuova lista parole");
		newNode->next=NULL;
		newNode->prev=NULL;
		*head=newNode;
		*tail=newNode;
		return;
		
	}
	
	while ((temp!=NULL)&& (temp->count > newNode->count)) {
		temp=temp->next;
	}
	if (temp==*head) {
		//printf("\n parola in testa");
		(*head)=newNode;
		temp->prev=newNode;
		newNode->next=temp;
		newNode->prev=NULL;
		return;
	}else if (temp==NULL) {
		//printf("\n parola in coda");
		(*tail)->next=newNode;
		newNode->prev=(*tail);
		newNode->next=NULL;
		(*tail)=newNode;
		return;
	}else {
		//printf("\n parola ordinata");
		newNode->next=temp;
		newNode->prev=temp->prev;
		temp->prev->next=newNode;
		temp->prev=newNode;
		return;
	}

	
	



}



struct elementonumerico *inserisciElemento(struct elementonumerico **lista, char* cod, char* word,int count){
	
	struct elementonumerico *listat9=*lista;
	if (listat9==NULL){
		listat9=(struct elementonumerico*)malloc(sizeof(struct elementonumerico));
		listat9->element2=NULL;
		listat9->element3=NULL;
		listat9->element4=NULL;
		listat9->element5=NULL;
		listat9->element6=NULL;
		listat9->element7=NULL;
		listat9->element8=NULL;
		listat9->element9=NULL;
		listat9->elementoprec=listat9;
		listat9->paroleHead=NULL;
		listat9->paroleTail=NULL;
	}
	int i;
	int c=strlen(cod);
	struct elementonumerico* temp=listat9;
	for(i=0;i<c;i++){
		struct elementonumerico *tempElement,*newnewElement;
		switch (cod[i]) {
			case '2':
				tempElement=temp->element2;
				break;
			case '3':
				tempElement=temp->element3;
				break;
			case '4':
				tempElement=temp->element4;
				break;
			case '5':
				tempElement=temp->element5;
				break;
			case '6':
				tempElement=temp->element6;
				break;
			case '7':
				tempElement=temp->element7;
				break;
			case '8':
				tempElement=temp->element8;
				break;
			case '9':
				tempElement=temp->element9;
				break;
			default:
				break;
		}
		
		if(tempElement==NULL){
	
		newnewElement=inserisciNuovoNodo(&newnewElement,temp);
			switch (cod[i]) {
				case '2':
					temp->element2=newnewElement;
					break;
				case '3':
					temp->element3=newnewElement;
					break;
				case '4':
					temp->element4=newnewElement;
					break;
				case '5':
					temp->element5=newnewElement;
					break;
				case '6':
					temp->element6=newnewElement;
					break;
				case '7':
					temp->element7=newnewElement;
					break;
				case '8':
					temp->element8=newnewElement;
					break;
				case '9':
					temp->element9=newnewElement;
					break;
				default:
					break;
			}
			
			
		temp=newnewElement;
		
		}
		else {
		
			switch (cod[i]) {
				case '2':
					temp=temp->element2;
					break;
				case '3':
					temp=temp->element3;
					break;
				case '4':
					temp=temp->element4;
					break;
				case '5':
					temp=temp->element5;
					break;
				case '6':
					temp=temp->element6;
					break;
				case '7':
					temp=temp->element7;
					break;
				case '8':
					temp=temp->element8;
					break;
				case '9':
					temp=temp->element9;
					break;
				default:
					break;
			}
		}
	}
	inserisciParola(&(temp->paroleHead),&(temp->paroleTail),word,count);
	return listat9;
}  


struct elementonumerico* initDictionary(char* path, Data d){
	FILE* fp;
	fp=fopen(path,"r");
	if (fp==NULL) {
		printf("errore in lettura file");
		return 0;
	}
	char* cod=(char*)malloc(wordlen*sizeof(char));
	char* word=(char*)malloc(wordlen*sizeof(char));
	char* count=(char*)malloc(wordlen*sizeof(char));
	while (1){
		if(fscanf(fp,"%s",cod)!=EOF){
		fscanf(fp,"%s",word);
		fscanf(fp,"%s",count);
		(d.listat9)=inserisciElemento(&(d.listat9),cod,word,atoi(count));
		}
		else
			break;
}
	fclose(fp);
	
	return d.listat9;
}

struct elementonumerico *findNodeFromCode(struct elementonumerico *fromNode, char toNode){

	struct elementonumerico *tempElement=fromNode;
	switch (toNode) {
		case '2':
			if((fromNode->element2)!=NULL)
			tempElement=fromNode->element2;
			break;
		case '3':
			if(fromNode->element3!=NULL)
			tempElement=fromNode->element3;
			break;
		case '4':
			if(fromNode->element4!=NULL)
			tempElement=fromNode->element4;
			break;
		case '5':
			if(fromNode->element5!=NULL)
			tempElement=fromNode->element5;
			break;
		case '6':
			if(fromNode->element6!=NULL)
			tempElement=fromNode->element6;
			break;
		case '7':
			if(fromNode->element7!=NULL)
			tempElement=fromNode->element7;
			break;
		case '8':
			if(fromNode->element8!=NULL)
			tempElement=fromNode->element8;
			break;
		case '9':
			if(fromNode->element9!=NULL)
			tempElement=fromNode->element9;
			break;
		default:
			break;
	}
	
	return tempElement;
	

}


char* substring_r(char* buffer, char* str, int start, int end)
{
	int i, x = 0;
	for(i = start ; i <= end; i++)
		buffer[x++] = str[i];
	buffer[x] = '\0';	
	return buffer;
}



char* findWord(char code, Data *d){
	
	char* str1=NULL;
	struct elementonumerico* temp=NULL;
	temp=findNodeFromCode(d->currentnode,code);
	if(temp!=d->currentnode)
		d->charnumber++;
	
	if((temp->paroleHead)!=NULL && (temp!=d->currentnode) && (strlen(temp->paroleHead->str)==(strlen(d->tempstring)+1))){
				
		d->currentnode=temp;
		d->currentword=d->currentnode->paroleHead;
		strcpy(d->tempstring,d->currentword->str);
		str1=d->currentnode->paroleHead->str;
	}
	
	else{
		d->currentnode=temp;
		char* nga;
		nga=(char*)malloc(4*sizeof(char));
		switch(code){
				
			case '2':
				nga="a";
				break;
				
			case '3':
				nga="d";
				break;
				
			case '4':
				nga="g";
				break;
				
			case '5':
				nga="j";
				break;
				
			case '6':
				nga="m";
				break;
				
			case '7':
				nga="p";
				break;
				
			case '8':
				nga="t";
				break;
				
			case '9':
				nga="w";
				break;
				
			default:
				break;
		}
		d->tempstring=strcat(d->tempstring,nga);
		str1=d->tempstring;
	}
  	return str1;
	
	
}



char* findNextWord(struct parola *fromNode,struct parola* head,Data *d){
	if(fromNode!=NULL){
		if (strcmp(d->currentword->str,d->tempstring)==0 && (strlen(d->tempstring)>0)) {
			
			if(fromNode->next!=NULL){
			d->currentword=fromNode->next;
			strcpy(d->tempstring,d->currentword->str);
			
				return fromNode->next->str;
			}
			else
				d->currentword=head;
				strcpy(d->tempstring,d->currentword->str);
				return head->str;
		}
	
		else{
			return d->tempstring;
		}
	}
	else{
		return d->tempstring;
	}
	
}
	
char* findPrevWord(Data *d){

	if(d->currentword==NULL){
		//Entra qui SOLO se non è mai stata trovata una parola nel dizionario//
		if(strlen(d->tempstring)==d->charnumber){
			//Se esiste il nodo ma non la lista della parole, scorre al nodo precedente e cancella una lettera//
			d->currentnode=d->currentnode->elementoprec;
			d->tempstring=substring_r(d->tempstring, d->tempstring, 0, strlen(d->tempstring)-2);
			d->charnumber--;
			return d->tempstring;
		}
		else {
			//Se non Esiste il nodo precedente cancella una lettera//			
			d->tempstring=substring_r(d->tempstring, d->tempstring, 0, strlen(d->tempstring)-2);
			return d->tempstring;
		}	
	}
	else{
		//Entra qui se durante la ricerca viene trovato un nodo contenente parole//
		if(strcmp(d->currentword->str,d->tempstring)!=0){
			//La parola visualizzata non è presente nel dizionario//
			if((d->currentnode->elementoprec->paroleHead)!=NULL && strlen(d->currentnode->elementoprec->paroleHead->str)==(strlen(d->tempstring)-1)){
				//Se il nodo precedenete esiste e contiene una parola, questa viene visualizzata//
				d->charnumber--;				
				d->currentnode=d->currentnode->elementoprec;			
				d->currentword=d->currentnode->paroleHead;
				strcpy(d->tempstring,d->currentword->str);
				return d->currentword->str;
			}
			
			if(strlen(d->tempstring)==d->charnumber){
				//Se esiste il nodo ma non la parola scorre al nodo precedente//
				d->currentnode=d->currentnode->elementoprec;
				d->charnumber--;
			}
			//e cancella un carattere
			d->tempstring=substring_r(d->tempstring, d->tempstring, 0, strlen(d->tempstring)-2);
			if(strlen(d->tempstring)==0){
				d->currentnode=d->listat9;
				d->charnumber=0;
				d->tempstring[0]='\0';
				d->currentword=NULL;
			}
			return d->tempstring;
			
		}
		else {
			//Entra qui se la parola visualizzata è contenuta nel dizionario
			if (strlen(d->currentword->str)>1) {
				//se questa contiene più di un carattere scorre al nodo precedente e se//
				d->currentnode=d->currentnode->elementoprec;
				d->charnumber--;
				if(d->currentnode->paroleHead!=NULL){
					//contiene una parola, questa viene visualizzata//
					d->currentword=d->currentnode->paroleHead;
					strcpy(d->tempstring,d->currentword->str);
					return d->currentword->str;
				}
				else {
					//non contiene una parola, viene cancellato un carattere//
					d->tempstring=substring_r(d->tempstring, d->tempstring, 0, strlen(d->tempstring)-2);
					return d->tempstring;
				}
			}
			else{
				//se questa ha un solo carattere viene cancellato il testo e riportato currentnode alla radice della lista//
				d->currentnode=d->listat9;
				d->charnumber=0;
				d->tempstring[0]='\0';
				d->currentword=NULL;
				return "errore";
			}
		}
	}
}



void SendEvent(XKeyEvent *event, int press){
	if (press)
		XSendEvent(event->display, event->window, True, KeyPressMask,(XEvent *)event);
	else
		XSendEvent(event->display, event->window, True, KeyReleaseMask,(XEvent *)event);
		XSync(event->display, False);
}


int sendChar(int c,int state){

	char disp[] = ":0";
	char *dp = getenv("DISPLAY");
	if (!dp)
		dp = disp;
	Display *dpy = XOpenDisplay(dp);
	if (dpy == NULL){
		return false;
	}
	else {
		Window cur_focus; // focused window
		int revert_to; // focus state
		// get window with focus
		XGetInputFocus(dpy, &cur_focus, &revert_to);
		if (cur_focus == None){
			return true;
		}
		else{
			XKeyEvent event;
			event.display = dpy;
			event.window = cur_focus;
			event.root = RootWindow(event.display,
			DefaultScreen(event.display));
			event.subwindow = None;
			event.time = CurrentTime;
			event.x = 1;
			event.y = 1;
			event.x_root = 1;
			event.y_root = 1;
			event.same_screen = True;
			event.type = KeyPress;
			event.state = state;
			event.keycode = XKeysymToKeycode(dpy,c);
			SendEvent(&event, true);
			event.type = KeyRelease;
			SendEvent(&event, false);
		}
		XCloseDisplay(dpy);
	}
	return true;
}


//These are CallBack routines associated to the keypad Buttons
void on_button1_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"1");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
		double elapsedtime=ElapsedTimer(d->begin);
		if((elapsedtime < 2) && (d->lastClickedButton==1)){
			d->lastClickedButton=1;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);
			if(d->tempchar[strlen(d->tempchar)-1]==','){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
				strcat(d->tempchar,";");
			}
			else if(d->tempchar[strlen(d->tempchar)-1]==';'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,".");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='.'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,":");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]==':'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"!");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='!'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"?");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='?'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"(");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='('){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,")");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]==')'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,",");
			}
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();					
		}

		else{
			d->lastClickedButton=1;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);		
			strcat(d->tempchar,",");
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();
		}
	}
}

void on_button2_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *caps = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonCaps"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"2");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==2)){
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='a'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"b");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='b'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"c");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='c'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"a");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=2;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"a");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('2',d);	
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);	
		}	
	}				
}

void on_button3_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"3");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}

	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==3)){
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='d'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"e");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='e'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"f");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='f'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"d");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=3;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"d");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
		const gchar* stringa=findWord('3',d);
		gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}
	}
}

void on_button4_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"4");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==4)){
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='g'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"h");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='h'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"i");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='i'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"g");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=4;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"g");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('4',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}
	}
}

void on_button5_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"5");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==5)){
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='j'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"k");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='k'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"l");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='l'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"j");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=5;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"j");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{	
			const gchar* stringa=findWord('5',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}	
	
	}
}

void on_button6_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"6");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==6)){
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='m'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"n");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='n'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"o");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='o'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"m");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=6;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"m");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('6',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}
	}
}


void on_button7_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"7");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==7)){
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='p'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"q");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='q'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"r");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='r'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"s");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='s'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"p");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=7;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"p");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('7',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);			
		}
	}
}

void on_button8_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"8");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==8)){
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='t'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"u");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='u'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"v");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='v'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"t");
				}
				
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=8;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"t");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('8',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}
	}

}

void on_button9_clicked(GtkButton *button, Data *d)
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"9");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
	else{	
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins))){
			double elapsedtime=ElapsedTimer(d->begin);
			if((elapsedtime < 2) && (d->lastClickedButton==9)){
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);
				if(d->tempchar[strlen(d->tempchar)-1]=='w'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
					strcat(d->tempchar,"x");
				}
				else if(d->tempchar[strlen(d->tempchar)-1]=='x'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"y");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='y'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"z");
				}
				else if (d->tempchar[strlen(d->tempchar)-1]=='z'){
					d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
					strcat(d->tempchar,"w");
				}
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();					
			}

			else{
				d->lastClickedButton=9;
				char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
				strcpy(d->tempchar,str);		
				strcat(d->tempchar,"w");
				gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
				d->begin=BeginTimer();
			}	
		}
		else{
			const gchar* stringa=findWord('9',d);
			gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);		
		}
	}	
}

void on_button0_clicked(GtkButton *button, Data *d)//At the moment it works only in numeric mode
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
		strcpy(d->tempnum,str);		
		strcat(d->tempnum,"0");
		gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempnum);	
	}
}


void on_buttoncancell_clicked(GtkButton *button, Data *d)//Switches words only in T9 mode
{
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	if(!(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num)))){
		const gchar* stringa=findNextWord(d->currentword,d->currentnode->paroleHead,d);
		gtk_entry_set_text(GTK_ENTRY(d->textarea),stringa);	
	}
}

void on_buttonasterix_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		double elapsedtime=ElapsedTimer(d->begin);
		if((elapsedtime < 2) && (d->lastClickedButton==10)){
			d->lastClickedButton=10;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);
			if(d->tempchar[strlen(d->tempchar)-1]=='*'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);		
				strcat(d->tempchar,"+");
			}
			else if(d->tempchar[strlen(d->tempchar)-1]=='+'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"-");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='-'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"/");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='/'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"=");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='='){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"$");
			}
			else if (d->tempchar[strlen(d->tempchar)-1]=='$'){
				d->tempchar=substring_r(d->tempchar, d->tempchar, 0, strlen(d->tempchar)-2);	
				strcat(d->tempchar,"*");
			}
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();					
		}

		else{
			d->lastClickedButton=10;
			char* str = (char*)gtk_entry_get_text(GTK_ENTRY(d->textarea));
			strcpy(d->tempchar,str);		
			strcat(d->tempchar,"*");
			gtk_entry_set_text(GTK_ENTRY(d->textarea),d->tempchar);
			d->begin=BeginTimer();
		}
	}
}


void on_buttonClear_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkEditable *entry = (GtkEditable*)d->textarea;	
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num))){
		if (strlen(str)==0){
			sendChar(0xff08,0);
			
		}

		else{
			char* strMinusLastChar=NULL;
			strMinusLastChar=(char*)malloc(wordlen*sizeof(char));			
			strcpy(strMinusLastChar,str);	
			strMinusLastChar=substring_r(strMinusLastChar, strMinusLastChar, 0, strlen(strMinusLastChar)-2);				
			gtk_entry_set_text(GTK_ENTRY(entry),strMinusLastChar);			
		}	

	}
	else{
		if (strlen(str)==0){
			sendChar(0xff08,0);
			
		}

		else if(strlen(str)==1){
			gtk_editable_delete_text(entry,0,-1);	
			char* stringa=findPrevWord(d);			
		}
		else{
			const gchar* stringa=findPrevWord(d);
			gtk_entry_set_text(GTK_ENTRY(entry),stringa);	
		}
	}
}

void on_buttonOK_clicked(GtkButton *button, Data *d)
{
	GtkWidget *num = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNum"));
	GtkEditable *entry = (GtkEditable*)d->textarea;	
	GtkWidget *manualins = GTK_WIDGET(gtk_builder_get_object(d->builder, "togglebuttonNew"));
	gint* position;	
	char* str=NULL;	
	str= gtk_editable_get_chars(entry,0,-1);
	
	if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(manualins)) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(num)) ){
		if(!hasNumber(str))
			d->listat9=inserisciElemento(&(d->listat9),getCodeFromString(str),str,1);
	}
	
	else{
		d->charnumber=0;
		if(d->currentword!=NULL)
			d->currentword->count++;
	}
		gtk_editable_delete_text(entry,0,-1);		
		int i=0;
		while(i<strlen(str)){
			sendChar(str[i],charToKeyCode(str[i]));
			i++;
		}
		sendChar(' ',0);
		
		d->currentnode=d->listat9;
		d->tempstring[0]='\0';
		d->tempnum[0]='\0';
		d->currentword=NULL;
		d->tempchar[0]='\0';
		d->lastClickedButton=-1;

}


//These routines are connected to signals but we don't use them cause we just need the state of the toggleButton 
void on_togglebuttonNew_toggled(GtkToggleButton *button, Data *data){}

void on_togglebuttonCaps_toggled(GtkToggleButton *button, Data *data){}

void on_togglebuttonNum_toggled(GtkToggleButton *button, Data *d){}
//________________________________________________________________________________________________________________

void on_togglebuttonLock_toggled(GtkToggleButton *button, Data *d)
{
	if(gtk_entry_get_visibility((GtkEntry*)d->textarea))	
		gtk_entry_set_visibility((GtkEntry*)d->textarea,FALSE);
	else
		gtk_entry_set_visibility((GtkEntry*)d->textarea,TRUE);
}


GtkWidget *get_object_from_builder(GtkBuilder *builder, gchar *obj_id){

	if(builder && obj_id){	
		GtkWidget *widget = GTK_WIDGET(gtk_builder_get_object(builder, obj_id));
		if(widget)return widget;
		else{
			g_printf("Object ID: %s not found.\n", obj_id);
			return NULL;
		}
	}
}


//This routine connect all the signals to the buttons
void add_all_events(Data *d){

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "window1")), "delete_event",
		G_CALLBACK(gtk_main_quit), NULL);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button1")), "clicked",
		G_CALLBACK(on_button1_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button2")), "clicked",
		G_CALLBACK(on_button2_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button3")), "clicked",
		G_CALLBACK(on_button3_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button4")), "clicked",
		G_CALLBACK(on_button4_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button5")), "clicked",
		G_CALLBACK(on_button5_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button6")), "clicked",
		G_CALLBACK(on_button6_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button7")), "clicked",
		G_CALLBACK(on_button7_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button8")), "clicked",
		G_CALLBACK(on_button8_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button9")), "clicked",
		G_CALLBACK(on_button9_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "button0")), "clicked",
		G_CALLBACK(on_button0_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttoncancell")), "clicked",
		G_CALLBACK(on_buttoncancell_clicked), d);
	
	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonasterix")), "clicked",
		G_CALLBACK(on_buttonasterix_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonClear")), "clicked",
		G_CALLBACK(on_buttonClear_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "buttonOK")), "clicked",
		G_CALLBACK(on_buttonOK_clicked), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonNew")), "toggled",
		G_CALLBACK(on_togglebuttonNew_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonCaps")), "toggled",
			G_CALLBACK(on_togglebuttonCaps_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonNum")), "toggled",
			G_CALLBACK(on_togglebuttonNum_toggled), d);

	g_signal_connect(G_OBJECT(get_object_from_builder(d->builder, "togglebuttonLock")), "toggled",
			G_CALLBACK(on_togglebuttonLock_toggled), d);

	g_signal_connect(GTK_STATUS_ICON(d->trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), d->the_window1);
    	
	g_signal_connect (G_OBJECT (d->the_window1), "destroy", G_CALLBACK(destroy), d);
    	
	g_signal_connect (G_OBJECT (d->the_window1), "delete_event", G_CALLBACK (delete_event), d->trayIcon);
   	
	g_signal_connect (G_OBJECT (d->the_window1), "window-state-event", G_CALLBACK (window_state_event), d->trayIcon);

	
	}

char* getCodeFromString(char *currentString){

	char *newCode=(char*)malloc((strlen(currentString)+1)*sizeof(char));
	int i=0;
	for (i=0; i<strlen(currentString); i++) {
		switch (currentString[i]) {
			case 'a':
			case 'b':
			case 'c':
				newCode[i]='2';
				break;
			case 'd':
			case 'e':
			case 'f':
				newCode[i]='3';
				break;
			case 'g':
			case 'h':
			case 'i':
				newCode[i]='4';
				break;
			case 'j':
			case 'k':
			case 'l':
				newCode[i]='5';
				break;
			case 'm':
			case 'n':
			case 'o':
				newCode[i]='6';
				break;
			case 'p':
			case 'q':
			case 'r':
			case 's':
				newCode[i]='7';
				break;
			case 't':
			case 'u':
			case 'v':
				newCode[i]='8';
				break;
			case 'w':
			case 'x':
			case 'y':
			case 'z':
				newCode[i]='9';
				break;
			default:
				break;
		}
	}
	newCode[i]='\0';
	return newCode;


}


void visitWordList(struct elementonumerico *node,FILE *fp){

	fprintf(fp,"%s %s %d\n", getCodeFromString(node->paroleHead->str),node->paroleHead->str,node->paroleHead->count);
	struct parola *tempWord=node->paroleHead;
	while (tempWord->next != NULL) {
	tempWord=tempWord->next;
	fprintf(fp,"%s %s %d\n", getCodeFromString(tempWord->str),tempWord->str,tempWord->count);
	}

}

void visit(struct elementonumerico *node,FILE *fp){

	if (node->paroleHead!=NULL) {
	visitWordList(node,fp);
	}else {

	}

}

void preorder(struct elementonumerico *node,FILE *fp,void (*visit)(struct elementonumerico *,FILE *fp)){
	
	if (node==NULL) {
		return;
	}	
	(*visit)(node,fp);
	preorder(node->element2,fp,visit);
	preorder(node->element3,fp,visit);
	preorder(node->element4,fp,visit);
	preorder(node->element5,fp,visit);
	preorder(node->element6,fp,visit);
	preorder(node->element7,fp,visit);
	preorder(node->element8,fp,visit);
	preorder(node->element9,fp,visit);
}


void saveDictionary(struct elementonumerico *listat9,char* path){
		FILE* fp;
	
		fp=fopen(path,"w");
		if (fp==NULL) {
			printf("errore in apertura file");
			return ;
		}
	fprintf(fp, "\n");
	preorder(listat9,fp,visit);
	fclose(fp);
}

int hasNumber(char* str){
	int i;
	for(i=0;i<strlen(str);i++){
	if(str[i]=='0' || str[i]=='1' || str[i]=='2' || str[i]=='3' || str[i]=='4' || str[i]=='5' || str[i]=='6' || str[i]=='7' || str[i]=='8' || str[i]=='9')
		return 1;
	if(str[i]==',' || str[i]=='.' || str[i]=='-' || str[i]=='\\' || str[i]=='+' || str[i]=='<')
		return 1;	
	if(charToKeyCode(str[i])!=0)
		return 1;
	}
	return 0;
}	


//These routines are associated to button's signal to manage minimizing the keypad and to put an icon on tray bar
static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}


static void destroy (GtkWidget *window, Data* data)
{
	saveDictionary(data->listat9,"./word_list.txt");
	gtk_main_quit ();
}

static gboolean delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
    return FALSE;
}

static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
{
    if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_widget_hide(GTK_WIDGET(widget));
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
    }
    else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    }
    return TRUE;
}
//__________________________________________________________________________________________________________________________________________________-


int charToKeyCode(char currentChar){

	int ascii=(int)currentChar;
if ((ascii>=33 && ascii<=42 && ascii!=39) || (ascii>=58 && ascii<=90 && ascii!=60 && ascii!=64) || ascii==47|| ascii==94 || ascii==95 || ascii==124 || ascii==130) {
		return 1;
	}
	if (ascii==35 || ascii==64 || ascii==91 || ascii==93) {
		return 2;
	}
	return 0;
}


